package com.adcc.monitoragent.show.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adcc.monitoragent.mina.MinaListener;
import com.adcc.monitoragent.util.JsonUtil;

public class ShowReceiveListener implements MinaListener  {
	private final static Logger log = LoggerFactory.getLogger(ShowReceiveListener.class);
	private List<Object> showClusterNames;
	
	public ShowReceiveListener(List<Object> showClusterNames) {
		this.showClusterNames = showClusterNames;
	}
	@Override
	public void receive(Object msg) {
		if(msg != null) {
			//过滤掉非本集群消息
			String temp = msg.toString();
			for (int i = 0; i < showClusterNames.size(); i++) {
				String name = showClusterNames.get(i).toString();
				if(temp.startsWith(name) && temp.replaceFirst(name, "").startsWith("com.")) {
					try {
						temp = temp.replaceFirst(name, "");
						System.out.println(name+","+temp);
						//temp转对象
						Object obj = JsonUtil.jsonToBean(temp);
						MessageService.watchMsg(name, obj);
						//跳槽循环，目前只支持监控2个集群
						log.info("收到监控消息：" + temp);
						break;
					} catch (Exception e) {
						log.error("数据添加失败" + msg + e.getMessage());
					}
				}
			}
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
